import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Dimension;
import client.ChatClient;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * This class constructs the GUI for user.  
 * @author G_14 
 * @version December 2011
 */

public class loginUI extends JPanel { //changed from JFrame to JPanel
	/**
	 * login button 
	 */
	private JButton login = null;
	/**
	 * password field  
	 */
	private JPasswordField jPassField = null;
	/**
	 * user name field  
	 */
	private JTextField jUserTextField = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private String pas1;
	private String name1;
	private int userType;
	/**
	 * back button 
	 */
	public JButton btnBack = null;
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;

//Constructors ****************************************************

/**
* Constructs an instance of the login UI.
*
* @param c- The connection.
*/
	public loginUI() {
		super();
		initialize();
	}
	public void SetUserType(int num){
		userType = num;
	}
	
	
	
//Instance methods ************************************************
	  
	/**
	* This method initialize the window.
	*/		
	private void initialize() {
		curPanel = this;
		this.setSize(400, 400);
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(16, 19, 263, 28));
			jLabel2.setText("Please enter your user name and password");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(16, 54, 72, 31));
			jLabel1.setText("User ID:");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(16, 92, 77, 27));
			jLabel.setText("Password:");
			btnBack = new JButton("Back");
			btnBack.setBounds(180, 268, 86, 25);
			this.add(btnBack,null);
			
			this.setLayout(null);
			this.add(getJPassField(), null);
			this.add(getButton(), null);
			this.add(getJUserTextField(), null);
			this.add(jLabel, null);
			this.add(jLabel1, null);
			this.add(jLabel2, null);
	}	

	  
	/**
	* This method define the continue button.
	* It has a listener and when an action is detected we create user 
	* instance and send it to the controller. After that create the next window
	* @return the button.
	*/	
	private JButton getButton() {
		if (login == null) {
			login = new JButton();
			login.setBounds(new Rectangle(152, 153, 92, 29));
			login.setText("continue");
			login.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent eb) {
					System.out.println("actionPerformed()"); 
					name1 = jUserTextField.getText();
					pas1 = jPassField.getText();
					User use = new User(name1,pas1);
				    Login l = new Login(userType);
				    l.setState(0);
				    Object mes= l.checkvalid(use);
				    jUserTextField.setText(null);
				    jPassField.setText(null);
				    
				    if (mes.equals("logSucces"))
				    {
				    	Global.logIn = l;
				    	if (userType == 1)
				    	{
				    		StudentUI studentUI = new StudentUI(name1);
							setVisible(false);
							System.out.println("Student window");
							Global.main.setContentPane(studentUI);
				    	}
				    		
				    	if (userType == 2)
				    	{
				    		ParentUI parentUI = new ParentUI(name1);
							setVisible(false);
							System.out.println("Parent window");
							Global.main.setContentPane(parentUI);
				    	}
				    	if (userType == 3)
				    	{
				    		TeacherUI teacherUI = new TeacherUI();
							setVisible(false);
							System.out.println("Teacher window");
							Global.main.setContentPane(teacherUI);
				    	}
				    }
				    
				}
			});
		}
		return login;
	}

	/**
	* This method define the text field for the username.
	* It has a listener and when an action is detected we initialize name1 with the input
	* @return the text field.
	*/		
	
	private JTextField getJUserTextField() 
	{
		if (jUserTextField == null) 
		{
			jUserTextField = new JTextField();
			jUserTextField.setBounds(new Rectangle(103, 58, 126, 23));
		}
		return jUserTextField;
				
	}
	
	/**
	* This method define the text field for the password.
	* It has a listener and when an action is detected we initialize pas1 with the input
	* @return the text field.
	*/	
	private JTextField getJPassField() {
		if (jPassField == null) {
			jPassField = new JPasswordField();
			jPassField.setBounds(new Rectangle(103, 92, 126, 23));
		}
		return jPassField;
	}	
} 